package org.dromara.hm.service.impl;

import org.apache.commons.math3.util.ArithmeticUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.hm.domain.bo.HmSaleOrderGiftBo;
import org.dromara.hm.domain.vo.saleOrder.HmSaleOrderGiftVo;
import org.dromara.hm.domain.HmSaleOrderGift;
import org.dromara.hm.mapper.saleOrder.HmSaleOrderGiftMapper;
import org.dromara.hm.service.IHmSaleOrderGiftService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单赠品明细Service业务层处理
 *
 * @author Hzy
 * @date 2025-03-17
 */
@RequiredArgsConstructor
@Service
public class HmSaleOrderGiftServiceImpl implements IHmSaleOrderGiftService {

    private final HmSaleOrderGiftMapper baseMapper;

    private final HmSupplementaryPickupRecordServiceImpl hmSupplementaryPickupRecordService;

    /**
     * 查询订单赠品明细
     *
     * @param giftId 主键
     * @return 订单赠品明细
     */
    @Override
    public HmSaleOrderGiftVo queryById(Long giftId){
        return baseMapper.selectVoById(giftId);
    }

    /**
     * 分页查询订单赠品明细列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 订单赠品明细分页列表
     */
    @Override
    public TableDataInfo<HmSaleOrderGiftVo> queryPageList(HmSaleOrderGiftBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HmSaleOrderGift> lqw = buildQueryWrapper(bo);
        Page<HmSaleOrderGiftVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的订单赠品明细列表
     *
     * @param bo 查询条件
     * @return 订单赠品明细列表
     */
    @Override
    public List<HmSaleOrderGiftVo> queryList(HmSaleOrderGiftBo bo) {
        LambdaQueryWrapper<HmSaleOrderGift> lqw = buildQueryWrapper(bo);
        List<HmSaleOrderGiftVo> hmSaleOrderGiftVos = baseMapper.selectVoList(lqw);
        // 查询当前赠品有无补充提货记录，若有则将其数量补充到当前赠品的已提数量中
        hmSaleOrderGiftVos.forEach(hmSaleOrderGiftVo -> {
            // 1. 查询补充提货
            BigDecimal num = hmSupplementaryPickupRecordService.getFulfilledQuantityForOriginalOrderProduct(hmSaleOrderGiftVo.getSaleId(), hmSaleOrderGiftVo.getGiftProductId());
            hmSaleOrderGiftVo.setGiftUsedNum(hmSaleOrderGiftVo.getGiftUsedNum().add(num));
        });
        return hmSaleOrderGiftVos;
    }

    private LambdaQueryWrapper<HmSaleOrderGift> buildQueryWrapper(HmSaleOrderGiftBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HmSaleOrderGift> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HmSaleOrderGift::getGiftId);
        lqw.eq(bo.getSaleId() != null, HmSaleOrderGift::getSaleId, bo.getSaleId());
        lqw.eq(bo.getGiftProductId() != null, HmSaleOrderGift::getGiftProductId, bo.getGiftProductId());
        lqw.eq(bo.getGiftNum() != null, HmSaleOrderGift::getGiftNum, bo.getGiftNum());
        lqw.eq(bo.getGiftUsedNum() != null, HmSaleOrderGift::getGiftUsedNum, bo.getGiftUsedNum());
        lqw.eq(bo.getGiftStatus() != null, HmSaleOrderGift::getGiftStatus, bo.getGiftStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), HmSaleOrderGift::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增订单赠品明细
     *
     * @param bo 订单赠品明细
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HmSaleOrderGiftBo bo) {
        HmSaleOrderGift add = MapstructUtils.convert(bo, HmSaleOrderGift.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setGiftId(add.getGiftId());
        }
        return flag;
    }

    /**
     * 修改订单赠品明细
     *
     * @param bo 订单赠品明细
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HmSaleOrderGiftBo bo) {
        HmSaleOrderGift update = MapstructUtils.convert(bo, HmSaleOrderGift.class);
        return baseMapper.updateById(update) > 0;
    }


    /**
     * 校验并批量删除订单赠品明细信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
